<?php
	class BoardMinutesView{
		private $m_URLBase;
		private $m_Posts;
		private $m_OrderBy;
		private $m_Order;
		private $m_PostsCount;
		private $m_PagesCount;
		private $m_CurrentPage;
		
		public function __construct($posts, $orderBy, $order, $currentPage){
			$this->m_URLBase = admin_url("admin.php?page=board-minutes", "http");
			$this->m_Posts = $posts;
			$this->m_OrderBy = $orderBy;
			$this->m_Order = $order;
			$this->m_PostsCount = wp_count_posts("board-minute")->publish;
			$this->m_PagesCount = ceil($this->m_PostsCount / BOARDMINUTES_PER_PAGE);
			$this->m_CurrentPage = $currentPage;
		}
		
		public function getContent(){
			$html = 
			'<div class="wrap">
				<div id="icon-generic" class="icon32 icon-generic">
					<br />
				</div>
				<h2>' . __("Board Minutes", BOARDMINUTES_TEXT_DOMAIN) . '</h2>'
				. $this->getPagination("top") . $this->getTable() . $this->getPagination("bottom") .
				'<br class="clear" />
			</div>';
			return $html;
		}
		
		private function getTable(){
			$html = 
			'<table class="wp-list-table widefat fixed posts" cellspacing="0">
				<thead>'
				. $this->getTableColumns() .
				'</thead>
				<tfoot>'
				. $this->getTableColumns() .
				'</tfoot>
				<tbody id="the-list">'
				. $this->getTableContent() .
				'</tbody>
			</table>';
			return $html;
		}
		
		private function getTableColumns(){
			$html = $this->getTableColumn("title", __("Title", BOARDMINUTES_TEXT_DOMAIN)) . $this->getTableColumn("date", __("Date", BOARDMINUTES_TEXT_DOMAIN));
			return $html;
		}
		
		private function getTableColumn($type, $title){
			$orderDirection = "asc";
			if($type == $this->m_OrderBy){
				$orderDirection = $this->m_Order == "asc"? "desc": "asc";
			}
			$html = 
			'<th scope="col" class="manage-column column-' . $type . ' sortable ' . $orderDirection  . '">
				<a href="' . $this->m_URLBase . '&#038;orderby=' . $type . '&#038;order=' . $orderDirection  . '">
					<span>' . $title . '</span>
					<span class="sorting-indicator"></span>
				</a>
			</th>';
			return $html;
		}
		
		private function getTableContent(){
			if(count($this->m_Posts) > 0 && $this->m_PostsCount > 0){
				return $this->getRows();
			}
			else{
				return $this->getNoItemsRow();
			}
		}
		
		private function getRows(){
			global $mode;
			$html = null;
			foreach($this->m_Posts as $post){
				$html .= 
				'<tr id="post-' . $post->ID . '" class="' . implode(" ", get_post_class("", $post->ID)) . '" valign="top">
					<td class="post-title page-title column-title">
						<strong>
							<a href="' . get_permalink($post->ID) . '" title="' . __("View", BOARDMINUTES_TEXT_DOMAIN) . ' &#8222;' . $post->post_title . '&#8220;" rel="permalink">' . $post->post_title . '</a>
						</strong>
					</td>
					<td class="date column-date"><abbr title="' . $this->getAbbreviatedTime($post) . '">' . apply_filters("post_date_column_time", $this->getHumanTime($post), $post, "date", $mode) . '</abbr><br />' . __("Published", BOARDMINUTES_TEXT_DOMAIN) . '</td>
				</tr>';
			}
			return $html;
		}
		
		private function getNoItemsRow(){
			$html = 
			'<tr class="no-items">
				<td class="colspanchange" colspan="2"> ' . __("No Board Minutes found.", BOARDMINUTES_TEXT_DOMAIN) . '</td>
			</tr>';
			return $html;
		}
		
		private function getAbbreviatedTime($post){
			$time = get_the_time(__("Y/m/d g:i:s A"), $post);
			return $time;
		}
		
		private function getHumanTime($post){
			$time = get_post_time("G", true, $post);
			$timeDiff = time() - $time;
			if($timeDiff > 0 && $timeDiff < DAY_IN_SECONDS){
				return sprintf(__("%s ago"), human_time_diff($time));
			}
			else{
				return mysql2date(__("Y/m/d"), $post->post_date);
			}
		}
		
		private function getPagination($location){
			$html = 
			'<div class="tablenav ' . $location . '">
				<div class="tablenav-pages">
					<span class="displaying-num">' . sprintf(_n("%d item", " %d items", $this->m_PostsCount, BOARDMINUTES_TEXT_DOMAIN), $this->m_PostsCount) . '</span>'
					. $this->getPaginationLinks() .
				'</div>
			</div>';
			return $html;
		}
		
		private function getPaginationLinks(){
			$html = null;
			if($this->m_PagesCount > 1){
				$leftButtonsStatus = " disabled";
				$rightButtonsStatus = " disabled";
				$previousPage = max(1, $this->m_CurrentPage - 1);
				$nextPage = min($this->m_PagesCount, $this->m_CurrentPage + 1);
				
				if($this->m_CurrentPage > 1){
					$leftButtonsStatus = null;
				}
				if($this->m_CurrentPage < $this->m_PagesCount){
					$rightButtonsStatus = null;
				}
				
				$html = '
				<span class="pagination-links">
					<a class="first-page' . $leftButtonsStatus . '" title="' . __("Go to the first page", BOARDMINUTES_TEXT_DOMAIN) . '" href="' . $this->m_URLBase . '">&laquo;</a>
					<a class="prev-page' . $leftButtonsStatus . '" title="' . __("Go to the previous page", BOARDMINUTES_TEXT_DOMAIN) . '" href="' . $this->m_URLBase . '&#038;paged=' . $previousPage . '">&lsaquo;</a>
					<span class="paging-input">' . $this->m_CurrentPage . ' ' . __("of", BOARDMINUTES_TEXT_DOMAIN) . ' <span class="total-pages">' . $this->m_PagesCount . '</span></span>
					<a class="next-page' . $rightButtonsStatus . '" title="' . __("Go to the next page", BOARDMINUTES_TEXT_DOMAIN) . '" href="' . $this->m_URLBase . '&#038;paged=' . $nextPage . '">&rsaquo;</a>
					<a class="last-page' . $rightButtonsStatus . '" title="' . __("Go to the last page", BOARDMINUTES_TEXT_DOMAIN) . '" href="' . $this->m_URLBase . '&#038;paged=' . $this->m_PagesCount . '">&raquo;</a>
				</span>';
			}
			return $html;
		}
	}
?>